#include "settings.h"
#include <QApplication>

Settings::Settings(QObject *parent) : QObject(parent)
  , settings(QSettings::IniFormat, QSettings::UserScope,
                                 QCoreApplication::organizationName(), QCoreApplication::applicationName())
{
}

QMutex Settings::mutex;
QAtomicPointer<Settings> Settings::instance = 0;

Settings* Settings::getInstance()
{
#ifndef Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
    if(!QAtomicPointer<Settings>::isTestAndSetNative())
        qDebug("Error: TestAndSetNative not supported!");
#endif

    if(instance.testAndSetOrdered(0, 0))
    {
        QMutexLocker locker(&mutex);
        instance.testAndSetOrdered(0, new Settings);
    }
    return instance;
}

QString Settings::getAdbPath() {
    QString path = settings.value("Settings/adbPath", "").toString();
    return path;
}

int Settings::getDisWidth() {
    int width = settings.value("Settings/disWidth", 360).toInt();
    return width;
}

void Settings::setAdbPath(QString path) {
    settings.beginGroup("Settings");
    settings.setValue("adbPath", path);
    settings.endGroup();
}

void Settings::setDisWidth(int width) {
    settings.beginGroup("Settings");
    settings.setValue("disWidth", width);
    settings.endGroup();
}
